package com.cdream.basic.algorithm.leetcode.string;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author cdream
 * @date 2022/3/14
 */
public class Question599 {
    public static void main(String[] args) {
        new Question599().findRestaurant(new String[]{"1","2","3","4"},
        new String[]{"4","3","2","1"});
    }
    public String[] findRestaurant(String[] list1, String[] list2) {
        Map<String,Integer> map = new HashMap<>();
        for(int i = 0; i<list2.length; i++){
            map.put(list2[i],i);
        }
        int minSum = 100000;
        List<String> res = new ArrayList();
        for(int i = 0; i<list1.length;i++){
            Integer index = map.get(list1[i]);
            if(index!=null){
                if(index + i <minSum){
                    res.clear();
                    minSum = index + 1;
                    res.add(list1[i]);
                }else if(index + i == minSum){
                    res.add(list1[i]);
                }
            }
        }
        return res.toArray(new String[]{});
    }
}
